// HealthDaoImpl.java
package com.community.dao.impl;
import com.community.dao.HealthDao;
import com.community.model.HealthInfo;
import com.community.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class HealthDaoImpl implements HealthDao {
    private static final String INSERT_SQL = "INSERT INTO health_info(elder_id,record_date,temperature,blood_pressure,symptoms) VALUES(?,?,?,?,?)";
    private static final String SELECT_BY_ELDER = "SELECT * FROM health_info WHERE elder_id=? ORDER BY record_date DESC";
    private static final String SELECT_RECENT = "SELECT * FROM health_info ORDER BY record_date DESC LIMIT ?";
    private static final String SELECT_ALL = "SELECT * FROM health_info ORDER BY record_date DESC";
    @Override
    public void insert(HealthInfo info) {
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(INSERT_SQL)) {
            pstmt.setInt(1, info.getElderId());
            pstmt.setTimestamp(2, Timestamp.valueOf(info.getRecordDate()));
            pstmt.setDouble(3, info.getTemperature());
            pstmt.setString(4, info.getBloodPressure());
            pstmt.setString(5, info.getSymptoms());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException("Insert failed", e);
        }
    }
    // 获取所有健康记录
    @Override
    public List<HealthInfo> getAll() {
        List<HealthInfo> allHealthRecords = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(SELECT_ALL);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                allHealthRecords.add(mapRow(rs));
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询所有健康记录失败", e);
        }
        return allHealthRecords;
    }
    @Override
    public List<HealthInfo> getByElderId(int elderId) {
        List<HealthInfo> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(SELECT_BY_ELDER)) {
            pstmt.setInt(1, elderId);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) list.add(mapRow(rs));
        } catch (SQLException e) {
            throw new RuntimeException("Query failed", e);
        }
        return list;
    }

    @Override
    public List<HealthInfo> getRecent(int limit) {
        List<HealthInfo> list = new ArrayList<>();
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(SELECT_RECENT)) {
            pstmt.setInt(1, limit);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) list.add(mapRow(rs));
        } catch (SQLException e) {
            throw new RuntimeException("Query failed", e);
        }
        return list;
    }

    private HealthInfo mapRow(ResultSet rs) throws SQLException {
        HealthInfo info = new HealthInfo();
        info.setId(rs.getInt("id"));
        info.setElderId(rs.getInt("elder_id"));
        info.setRecordDate(rs.getTimestamp("record_date").toLocalDateTime());
        info.setTemperature(rs.getDouble("temperature"));
        info.setBloodPressure(rs.getString("blood_pressure"));
        info.setSymptoms(rs.getString("symptoms"));
        return info;
    }
}